package managedbean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import jpa.PetJPA;
import jpa.ShoppingCart;

/**
 * Managed Bean ShowShoppingCartMBean
 */
@ManagedBean(name = "shopCart")
@SessionScoped
public class ShowShoppingCartMBean {
	private static final long serialVersionUID = 1L;

	protected ShoppingCart cart;
	protected Collection<PetJPA> petsShoppingCartView;
	private int screen = 0;
	
	public int getNumberPets()
	{ 
		FacesContext fc = FacesContext.getCurrentInstance();
		ShoppingCart cart = (ShoppingCart)fc.getExternalContext().getSessionMap().get("shoppingCart");
		return cart.getPets().size();
	}

	/**
	 * allows forward or backward in user screens
	 */
	public void nextScreen()
	{
		if (((screen+1)*5 < cart.getPets().size()))
		{
			screen +=1;
		}
	}
	public void previousScreen()
	{
		if ((screen > 0))
		{
			screen -=1;
		}
	}

	public Collection<PetJPA> getPetsShoppingCartView() {
		cart = (ShoppingCart)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("shoppingCart");
		int n =0;
		petsShoppingCartView = new ArrayList<PetJPA>();
		for (Iterator<PetJPA> iter2 = cart.getPets().iterator(); iter2.hasNext();)
		{
			PetJPA pet2 = (PetJPA) iter2.next();
			if (n >= screen*5 && n< (screen*5+5))
			{				
				this.petsShoppingCartView.add(pet2);
			}
			n +=1;
		}
		return petsShoppingCartView;
	}
	
}
